<script lang="ts">
import { defineComponent, ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getClassDes, getClassVideo, getAllseries, getClassList } from '../api/class/classdes'
import { useStore } from '../stores/class'
import { showToast, Toast } from 'vant'
import { storeToRefs } from 'pinia'

export default defineComponent({
  components: {},
  setup() {
    let router: any = useRouter()
    let route: any = useRoute()
    const store = useStore()
    const { addCollect, unCollect } = store
    // const { payClass, CollectClassList } = storeToRefs(store)

    let groupId = route.query.groupId
    let paymentflag = route.query.payment
    let active = ref(0)
    let classDes: any = ref(null)
    let intro: any = ref(null)
    let newintro: any = ref(null)
    let seriesid: any = ref(null)
    let freeSeries: any = ref({})
    let fiveSeries: any = ref(null)
    let moreflag = ref(false)
    let videourl = ref<string | null>()
    let price = route.query.price
    let payflag = ref(false)
    let Collectflag = ref(false)
    let token = localStorage.getItem('token');

    let buyClass: any = (): any => {
      if (token != undefined) {
        router.push({
          path: '/classorder',
          query: {
            groupId,
            seriesid: seriesid.value,
            price: route.query.price
          }
        })
      }
      else {
        showToast('请登录');

        router.push({
          path: '/register',
        })
      }

    }
    let istoken = localStorage.getItem('token') != null;
    console.log('istoken=>', istoken)
    let back = () => {
      if (route.query.val == undefined) {
        router.push({
          path: '/class'
        })
      } else {
        router.push({
          path: '/searchclass',
          query: {
            val: route.query.val
          }
        })
      }

    }
    let payClassJson: any = window.localStorage.getItem("payClass");
    let payClass = ref(JSON.parse(payClassJson) || []);
    let CollectClassListJson: any = ref(window.localStorage.getItem("CollectClassList"));
    let CollectClassList :any= ref(JSON.parse(CollectClassListJson.value) || []);
    let getClassDesFun = (groupId: any) => {
      getClassDes(groupId).then((data: any) => {
        console.log('课程详情数据=>', data)
        classDes.value = data.data
        console.log('课程详情数据classDes=>', classDes.value)
        intro.value = classDes.value.intro
        // console.log("intro=>", intro.value);
        newintro.value = intro.value.replace(/width="*\d+/g, 'width=375')
        newintro.value = newintro.value.replace(/height="*\d+/g, 'height=auto')
        newintro.value = newintro.value.replace(/<p+/g, "<p style='padding:0 20px;font-size:14px;line-height:40px'")
        newintro.value = newintro.value.replace(
          /<strong+/g,
          "<strong style='padding:0 0px;font-size:17px;'"
        )
        newintro.value = newintro.value.replace(
          /<h2+/g,
          "<h2 style='padding:0 40px;font-size:16px;'"
        )
        // console.log("newintro=>", newintro.value);
      })
    }
    //课程数据
    let getClassVideoFun = (videoid: any) => {
      getClassVideo(videoid).then((data: any) => {
        console.log('视频课程数据=>', data)
        videourl.value = data.data.videoUrl
        console.log('videourl.value=>', videourl.value)
      })
    }
    //获得seriesid
    let getAllseriesFun = (groupId: any) => {
      getAllseries(groupId).then((data: any) => {
        console.log('课程seriesid数据=>', data)
        seriesid.value = data.data.items[0].seriesId
        console.log('seriesid=>', seriesid.value)
        getClassListFun(seriesid.value, 10)
      })
    }
    //获得课程数据
    let getClassListFun = (seriesid: number, limit: any) => {
      getClassList(seriesid, limit).then((data: any) => {
        console.log('所有章节数据=>', data)
        freeSeries.value = data.data
        console.log('所有章节数据freeSeries=>', freeSeries.value)
        // fiveSeries.value = freeSeries.value.items.filter((item: any, index: number) => {
        //   return index < 5;
        // })
      })
    }
    let more = () => {
      getClassListFun(seriesid.value, '')
      moreflag.value = !moreflag.value
    }
    //点击观看视频
    let click_video = (vid: number, index: number) => {
      console.log('index=>', index)
      if (index < 2) {
        //点击了前两个课程
        router.push({
          path: '/classvideo',
          query: {
            groupId,
            vid,
            seriesid: seriesid.value,
            price: route.query.price
          }
        })
        console.log('点击了章节', vid)
        getClassVideoFun(vid)
      } else {
        //点击了后两个课程
        if (payflag.value == true) {
          //判断是否报名，若已报名
          router.push({
            path: '/classvideo',
            query: {
              groupId,
              vid,
              seriesid: seriesid.value,
              price: route.query.price
            }
          })
          console.log('点击了章节', vid)
          getClassVideoFun(vid)
        } else {
          //没有报名
          showToast('请报名后观看')
        }
      }
    }
    let startStudy = () => {
      console.log('点击了开始学习')
      console.log(freeSeries.value)
      click_video(freeSeries.value.items[0].videoInfo.videoId, 0)
    }
    
    //点击添加收藏
    console.log('store.CollectClassList=>', store.CollectClassList);
    watch(CollectClassList, () => {
      // console.log('store.CollectClassList=>', store.CollectClassList)
      let index = CollectClassList.value.findIndex((item: any) => {
        return item.data.groupId == route.query.groupId
      })
      console.log('是否有该课程数据=>', index)
      if (index == -1) {
        Collectflag.value = false
      } else {
        Collectflag.value = true
      }
    })

    onMounted(() => {
      getClassDesFun(groupId)
      getClassVideoFun(15100525415798)
      getAllseriesFun(groupId)
      paymentflag = route.query.payment
      payClass.value.forEach((item: any) => {
        if (Number(item.groupId) == Number(route.query.groupId)) {
          if (item.payment == 1) {
            payflag.value = true
            return
          }
        }
      })
      console.log('store.CollectClassList=>', store.CollectClassList);
      if (store.CollectClassList) {
        let index = CollectClassList.value.findIndex((item: any) => {
          return item.data.groupId == route.query.groupId
        })
        console.log('是否有该课程数据=>', index);
        if (index == -1) {
          Collectflag.value = false
        } else {
          Collectflag.value = true
        }
      }
    })



    // https://www.cctalk.com/webapi/im/v1.3/group/86370728/info?isRichInfo=true&_timestamp=1686121670762
    return {
      active,
      buyClass,
      back,
      classDes,
      newintro,
      freeSeries,
      fiveSeries,
      more,
      moreflag, //是否显示更多按钮
      paymentflag, //是否支付完毕
      click_video, //点击章节方法
      price, //课程价格
      payflag, //是否报名
      startStudy, //点击开始学习
      addCollect,
      Collectflag,
      unCollect,
      istoken
    }
  }
})
</script>
<template>
  <div class="class-all-des" v-if="classDes">
    <div class="desbar">
      <span class="iconfont icon-xiangzuojiantou" @click="back"> </span>
      <h2>课程详情</h2>
    </div>
    <!-- 课程视频 -->
    <div class="class-video">
      <img :src="classDes.avatar" alt="" />
      <div class="float">
        <span class="iconfont icon-shengyinka"></span>
        <span v-if="!payflag" @click="startStudy">免费试学</span>
        <span v-else @click="startStudy">开始学习</span>
      </div>
    </div>
    <!-- 课程的详细信息 -->
    <div class="descript">
      <!-- 课程价格 -->
      <div class="class-price">￥{{ price }}</div>
      <!-- 课程名字 -->
      <div class="class-name">{{ classDes.groupName }}</div>
      <!-- 报名人数、课时、 -->
      <div class="class-count">
        报名人数<span>{{ classDes.memberCount }}</span> 课时 <span>{{ classDes.videoCount }}</span>
      </div>
      <!-- 课程亮点 -->
      <div class="lighthight">
        亮点:
        <span v-for="(item, index) in classDes.sellPoint" :key="index">{{
          item.sellPointName
        }}</span>
      </div>
      <!-- 售后服务 -->
      <div class="tips">
        <span>7天内协商退款</span>
        <span v-if="classDes.effectiveEndTime">有效期至{{ classDes.effectiveEndTime.match(/^\d{4}-\d{2}-\d{2}/)[0] }}</span>
      </div>
    </div>
    <!-- 课程简介与免费试学 -->
    <div class="intro">
      <van-tabs v-model:active="active" scrollspy sticky :offset-top="44">
        <van-tab title="简介" title-class="tab1">
          <div class="info" v-html="newintro"></div>
        </van-tab>
        <van-tab title="课程" title-class="tab1">
          <div class="title">
            课程
            <span>({{ classDes.videoCount }})</span>
          </div>
          <!-- 免费试学 -->
          <div class="freeclass" v-if="freeSeries.items">
            <div class="free-class-video">
              <div class="image">
                <img :src="classDes.avatar" alt="" />
              </div>
              <div class="word">
                {{ classDes.groupName }}
                <p>{{ classDes.videoCount }}课时</p>
              </div>
            </div>
            <!-- 试学列表 -->
            <div class="free-class-list">
              <ul>
                <li v-for="(item, index) in freeSeries.items" @click="click_video(item.videoInfo.videoId, index)">
                  <div class="smalltag" v-if="index < 2 && !payflag">试学</div>
                  <div class="nulltag" v-else-if="index >= 2 && !payflag"></div>
                  <div class="free-right">
                    <div class="free-class-name one-text-cut">{{ item.videoInfo.videoName }}</div>
                    <div class="free-count">{{ item.videoInfo.playCount }}次播放</div>
                  </div>
                </li>
              </ul>
            </div>
            <div class="more" @click="more" v-if="!moreflag && freeSeries.items.length >= 10">
              查看更多课程
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
    <div class="class-fixed-bottom">
      <div class="logo">
        <!-- <div class="logo-btn">
          <span class="iconfont icon-review"></span>
          联系
        </div> -->
        <div class="logo-btn">
          <span class="iconfont icon-31shoucang" @click="addCollect(classDes, price)"
            v-if="!Collectflag || !istoken"></span>
          <span class="iconfont icon-wodeshoucang" @click="unCollect(classDes)" v-else style="color: yellow"></span>
          收藏
        </div>
      </div>
      <div>
        <div class="class-btn" @click="buyClass" v-if="!payflag">
          <div>
            <span v-if="price">{{ '￥' + price }}</span>立即报名
          </div>
        </div>
      </div>
      <div class="finishpay" v-if="payflag">
        <span>已报名</span>
      </div>
    </div>

    <div class="null"></div>
  </div>
</template>
<style lang="scss" scoped>
.class-all-des {
  background-color: #f6f7f9;
  height: 100%;
  min-height: 667px;
}

.desbar {
  display: flex;
  line-height: 44px;
  padding: 0 10px;
  position: sticky;
  z-index: 10;
  top: 0;
  background-color: #f5f5f5;

  span {
    font-size: 20px;
    font-weight: 600;
  }

  h2 {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;

    // margin-left: 50px;
  }
}

.class-video {
  width: 100%;
  position: relative;

  img {
    width: 100%;
    height: auto;
  }

  .float {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-image: linear-gradient(to bottom right, #80d1c2, #63abff, #9a4efc);
    color: #fff;
    padding: 6px 15px;
    border-radius: 20px;
    font-size: 14px;
  }
}

.class-price {
  color: #ff3473;
  font-size: 19px;
  line-height: 38px;
  font-weight: 600;
}

.descript {
  margin: 10px 8px;
  background-color: #fff;
  border-radius: 20px;
  padding: 7px 15px;
}

.class-name {
  line-height: 40px;
  font-size: 20px;
  font-weight: 600;
}

.class-count {
  color: #95a1af;
  line-height: 28px;
  font-size: 14px;

  span {
    color: #2f3742;
    margin-right: 28px;
  }
}

.lighthight {
  height: 42px;
  font-size: 14px;
  display: flex;
  align-items: center;

  span {
    display: inline-block;
    padding: 2px 9px;
    background-color: #eff2ff;
    color: #2f5aff;
    margin-left: 5px;
    border: 1px solid #c8d4ff;
    border-radius: 5px;
    font-size: 12px;
  }
}

.tips {
  font-size: 12px;
  color: #9da8b5;
  line-height: 23px;

  span {
    margin-right: 10px;
  }
}

.intro {
  background-color: #f6f7f9;

  .info {
    img {
      width: 375px !important;
      height: auto !important;
    }
  }

  .tab1 {
    background-color: #f6f7f9;
  }

  ::v-deep .van-tab {
    background-color: #f6f7f9;
  }

  .title {
    margin: 0 16px;
    line-height: 38px;
    font-size: 20px;
    font-weight: 600;
    margin-top: 10px;

    span {
      color: #687583;
      font-size: 14px;
      margin-left: 8px;
      margin-top: -3px;
    }
  }

  .freeclass {
    margin: 0 8px;
    border-radius: 10px;
    overflow: hidden;

    .free-class-video {
      display: flex;
      background-color: #fff;
      padding: 10px;

      .image {
        img {
          width: 110px;
          height: auto;
          border-radius: 10px;
        }
      }

      .word {
        color: #2f3742;
        margin-left: 14px;
        padding-right: 48px;
        font-size: 16px;
        font-weight: 600;
        line-height: 22px;
        display: flex;
        flex-direction: column;
        justify-content: center;

        p {
          color: #95a1af;
          font-size: 12px;
          line-height: 36px;
        }
      }
    }

    .free-class-list {
      ul {

        // padding:0 10px;
        li {
          display: flex;
          background-color: #fff;
          padding: 0 10px;
          padding-bottom: 17px;

          .smalltag {
            color: #ff3271;
            border: 1px solid #ffa8c3;
            background-color: #ffeff4;
            width: 26px;
            padding: 2px 5px;
            height: 14px;
            font-size: 12px;
            text-align: center;
            line-height: 14px;
            border-radius: 5px;
            margin-right: 6px;
            margin-top: 7px;
          }

          .nulltag {
            @extend .smalltag;
            background-color: transparent;
            border-color: transparent;
          }

          .free-right {
            flex: 1;
          }

          .free-class-name {
            font-size: 16px;
            color: #2f3742;
            line-height: 28px;
            width: 260px;
          }

          .free-count {
            font-size: 12px;
            color: #95a1af;
            line-height: 23px;
          }
        }
      }
    }
  }

  .more {
    text-align: center;
    color: #63abff;
    line-height: 40px;
  }
}

.class-fixed-bottom {
  height: 62px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  z-index: 100;
  background-color: #fff;

  .logo {
    font-size: 20px;
    display: flex;
    flex: 1;
    justify-content: center;

    .logo-btn {
      display: flex;
      flex-direction: column;
      font-size: 14px;
      width: 50%;
      text-align: center;

      span {
        font-size: 20px;
      }
    }
  }

  .class-btn {
    background-color: #ff3e8f;
    color: #fff;
    width: 236px;
    height: 44px;
    font-size: 14px;
    font-weight: 600;
    text-align: center;
    line-height: 44px;
    border-radius: 20px;
    margin-right: 13px;
    margin-left: 15px;
  }

  .finishpay {
    @extend .class-btn;
    background-color: #dd7ba4;
  }
}

.null {
  height: 62px;
  width: 100%;
}
</style>